/**
 * 格式化数字为指定长度的字符串，不足部分补零
 *
 * @param {number|string} val - 要格式化的数值或字符串
 * @param {number} [len=2] - 格式化后的字符串长度，默认为2
 * @returns {string} - 格式化后的字符串
 */
function pz(val, len = 2) {
  const strVal = String(val);
  if (!strVal) {
    return strVal;
  }
  if (strVal.length >= len) {
    return strVal;
  }
  return `${new Array(len - strVal.length + 1).fill("0").join("")}${strVal}`;
}
const _t = new Date();
export const _hash = `${_t.getFullYear()}${pz(_t.getMonth() + 1)}${pz(_t.getDate())}${pz(
  _t.getHours(),
)}${pz(_t.getMinutes())}${pz(_t.getSeconds())}${pz(_t.getMilliseconds(), 3)}`;

/**
 * 注入脚本函数
 *
 * @returns 返回包含JavaScript脚本的字符串
 */
export function injectScript() {
  return `<script>console.log('inject script')</script>`;
}

// wmda - 埋点
export function injectWMDA(extendConfig) {
  // const domain = window?.document?.domain || location?.host || ''
  const WMDA_SDK_CONFIG = {
    api_v: 1,
    sdk_v: 0.1,
    mode: "report",
    // cookie_domain: domain,
    cate_id: "",
    page_extra: [
      {
        key: "",
        value: "",
      },
    ],
    user_extra: [
      {
        key: "",
        value: "",
      },
    ],
    channel: "", // 按需填写
    getUserId() {}, // 按需填写
    getDeviceId() {}, // 按需填写
    SPA_HASH: true, // 单页应用必选
    ...extendConfig,
  };
  const _tempStr = `
  <script type="text/javascript">
    window.WMDA_SDK_CONFIG = (${JSON.stringify(WMDA_SDK_CONFIG)});
  </script>
  <script type="text/javascript" src="http://j1.58cdn.com.cn/wmda/js/statistic.js"></script>
  `;
  return _tempStr;
}
